<?php

namespace ElementVip\Shop\Backend\Model;

use DB;
use Illuminate\Database\Eloquent\Model;
use ElementVip\Shop\Backend\Model\Common;

/**
 * 门店后台Model功能
 */
class Store extends Model
{

	protected $table   = 'storelocator';
	protected $guarded = ['id'];

	public function cityInfo()
	{
		return $this->hasOne('ElementVip\Shop\Backend\Model\City', 'id', 'city');
	}

	/**
	 * 获取单条门店
	 *
	 * @param type $id
	 *
	 * @return type
	 */
	public static function getStorelocator($id)
	{
		/* 填充数据 */
		$data = (object) ['id'      => 0, 'name' => null, 'city' => null, 'address' => null, 'phone' => null, 'top' => '0', 'tag' => [],
		                  'img_url' => null, 'code' => null, 'type' => null, 'act' => null, 'is_enable' => '1'];
		if (is_numeric($id)) {
			$data      = DB::table('storelocator')->where('id', $id)->first();
			$data->tag = unserialize($data->tag);
		}
		if ($data === null) {
			abort(404);
		}

		return $data;
	}

	public static function saveSearchs($ids)
	{
		DB::statement('truncate table storelocator_search');
		if (!empty($ids)) {
			foreach ($ids as $key => $id) {
				$storelocator            = self::getStorelocator($id);
				$data[$key]['city']      = $storelocator->city;
				$data[$key]['shop_id']   = $storelocator->id;
				$data[$key]['shop_name'] = $storelocator->name;
			}
			DB::table('storelocator_search')->insert($data);
		}
	}

	public static function getSearchs()
	{
		return DB::table('storelocator_search')->get();
	}

	/**
	 * 获取门店城市列表(key-value)
	 *
	 * @return type
	 */
	public static function getStorelocatorCityListKV()
	{
		$new_list = [];
		$list     = DB::table('storelocator_citys')->get();
		foreach ($list as $value) {
			$new_list[$value->id] = $value->name;
		}

		return $new_list;
	}

	/**
	 * 获取活动的门店
	 *
	 * @param type $id
	 *
	 * @return type
	 */
	public static function getStorelocatorAct_City_Shop()
	{
		$data  = DB::table('storelocator')->where('is_enable', 1)->get();
		$citys = self::getStorelocatorCityListKV();
		foreach ($data as $value) {
			$new_data[$citys[$value->city]][] = $value;
		}
		ksort($new_data);

		return $new_data;
	}

	/**
	 * 快速导入功能
	 *
	 * @param type $results
	 */
	public static function quickImport($results)
	{
		$new_data = [];
		foreach ($results as $value) {
			if (!$value[0]) {
				continue;
			}

			$edit              = [];
			$edit['name']      = $value[3];
			$edit['city']      = self::defaultCityToLetter(trim($value[2]));
			$edit['address']   = trim($value[4]);
			$edit['phone']     = isset($value[5]) ? $value[5] : '';
			$edit['top']       = false;
			$edit['tag']       = isset($value[8]) ? self::getTagCode($value[8]) : '';
			$edit['code']      = isset($value[0]) ? $value[0] : '';
			$edit['type']      = isset($value[1]) ? $value[1] : '';
			$edit['act']       = 0;
			$edit['is_enable'] = true;
			$edit['img']       = '';
			$edit['img_url']   = '';
			$edit['lat']       = isset($value[6]) ? $value[6] : '';
			$edit['lng']       = isset($value[7]) ? $value[7] : '';

			$new_data[] = $edit;
		}

		foreach ($new_data as $val) {
			$geocoder = Common::_store_qq_geocoder($val['address']);
			if (is_object($geocoder)) {
				if ($geocoder->status === 0) {
					$val['lat'] = $geocoder->result->location->lat;
					$val['lng'] = $geocoder->result->location->lng;
				}
			}
			$result = DB::table('storelocator')->where('code', $val['code'])->first();
			if ($result !== null) {
				DB::table('storelocator')->where('code', $val['code'])->update($val);
			} else {
				DB::table('storelocator')->insert($val);
			}
		}
	}

	/**
	 * 写入新城市
	 *
	 * @param type $cityname
	 *
	 * @return type
	 */
	private static function defaultCityToLetter($cityname)
	{
		$citys = self::getStorelocatorCityListKV();
		if (in_array($cityname, $citys)) {
			return array_search($cityname, $citys);
		} else {
			$data['name']   = $cityname;
			$data['letter'] = Common::getFirstLetter($cityname);
			$geocoder       = Common::_store_qq_geocoder($data['name']);
			if ($geocoder->status === 0) {
				$data['lat'] = $geocoder->result->location->lat;
				$data['lng'] = $geocoder->result->location->lng;
			}

			return DB::table('storelocator_citys')->insertGetId($data);
		}
	}

	/**
	 * 获取TAG的代码
	 *
	 * @param type $code
	 *
	 * @return type
	 */
	public static function getTagCode($code)
	{
		$return_codes = [];
		if (empty($code)) {
			return null;
		}
		$codes  = explode(',', $code);
		$result = DB::table('storelocator_tags')->whereIN('name', $codes)->get();
		if (!empty($result)) {
			foreach ($result as $value) {
				$return_codes[] = $value->id;
			}

			return implode(',', $return_codes);
		} else {
			return null;
		}
	}

}
